package com.crm.controller.ordersController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.PartsDAO;
import com.crm.dao.StockDAO;
import com.crm.entity.Business;
import com.crm.entity.Parts;
import com.crm.entity.StockEntity;


@Controller
@RequestMapping("/inventory")
public class InventoryController {
	protected final Log logger = LogFactory.getLog(getClass());

	PartsDAO partsDAO=new PartsDAO();
	StockDAO stockDAO=new StockDAO();
	@RequestMapping( "/show.do") 
	public ModelAndView show(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		StockDAO stdao=new StockDAO();
				List<StockEntity> lse = null;
					try {
						lse =stdao.findAll(busid);
					} catch (Exception e) {
						e.printStackTrace();
					}
					
				return model = new ModelAndView("/jsp/inventory.jsp")
				.addObject("submenu", "orders")
				.addObject("menuName","orders")
				.addObject("stockList",lse);
	}
	
	@RequestMapping( "/update.do") 
	public ModelAndView update(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		String sRowCount=request.getParameter("rowCount");
		StockDAO stdao = new StockDAO();
		List<StockEntity> stockList=new ArrayList<StockEntity>();
		StockEntity se=new StockEntity();
		if (sRowCount!=null) {
			int c = Integer.parseInt(sRowCount);
			for (int j = 1; j <=c; j++) {
				String sid = request.getParameter("id" + j );
				String spartId = request.getParameter("partId" + j );
				String description = request.getParameter("desc" + j );
				String sQty = request.getParameter("remainingQty" + j );	
				String sPurchaseDate = request.getParameter("purcDate" + j );	
				String sLastUsedDate = request.getParameter("lastUsed" + j );	
				String sProcQty = request.getParameter("procQty" + j );	
				String sSoldQty = request.getParameter("soldQty" + j );	
				if(sid!=null && sQty!=null){
					int id = Integer.parseInt(sid);
					int partId = Integer.parseInt(spartId);
					int qty = Integer.parseInt(sQty);
					int procQty = Integer.parseInt(sProcQty);
					int soldQty = Integer.parseInt(sSoldQty);
					
					SimpleDateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");
					java.util.Date purchasedDate=null;
					java.util.Date lastUsedDate=null;
					try{
						purchasedDate=formatter.parse(sPurchaseDate);
						lastUsedDate=formatter.parse(sLastUsedDate);
					}catch(Exception e){
						e.printStackTrace();
					}
					//Parts part=partsDAO.findByPartId(partId, bus.getBusinessId());
					Parts part=new Parts();
					part.setPartid(partId);
					se=new StockEntity(id,part,description,qty,purchasedDate,lastUsedDate,procQty,soldQty,bus);
					stockList.add(se);
				}
			}
			stdao.insertStockList(stockList);
		}
		return model = new ModelAndView("redirect:show.do");
	}

	@RequestMapping("/addPart.do")
	public ModelAndView addPart(ModelAndView model,HttpServletRequest request,HttpSession session){
		Business business=(Business)session.getAttribute("business");
		StockEntity se=new StockEntity();
		SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
		
		String partName=request.getParameter("partName");
		String description=request.getParameter("description");
		Parts parts=null;
		try {
			parts=partsDAO.findByPart(partName, business.getBusinessId());
			if(parts==null){
				parts=new Parts();
				parts.setPrtnum(partName);
				parts.setDescription(description);
				partsDAO.insert(parts);
				parts=partsDAO.findByPart(partName, business.getBusinessId());
			}
			se.setPartNo(parts);
		} catch (Exception e) {
			e.printStackTrace();
		}
		se.setBusinessId(business);
		se.setDescription(description);
		String remainingQty=request.getParameter("remainingQty");
		if(remainingQty!=null)se.setQuantity(Integer.parseInt(remainingQty));
		String purchasedDate=request.getParameter("purchasedDate");
		if(purchasedDate!=null){
			try {
				se.setPurchaseDate(sdf.parse(purchasedDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		String lastUsedDate=request.getParameter("lastUsedDate");
		if(lastUsedDate!=null){
			try {
				se.setLastUsedDate(sdf.parse(lastUsedDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		String procuredQty=request.getParameter("procuredQty");
		if(procuredQty!=null)se.setProcuredQuantity(Integer.parseInt(procuredQty));
		String soldQty=request.getParameter("soldQty");
		if(soldQty!=null)se.setSoldQuantity(Integer.parseInt(soldQty));
		try {
			stockDAO.insert(se);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model=new ModelAndView("redirect:show.do","msg","Succesfully Added To Inventory");
	}
	}
